<!-- 头部 -->
<template>
  <div class="header">
    <!-- 顶部导航栏 -->
    <div class="nav">
      <div class="nav-info">
        <p>商城优惠大酬宾，全场满59元包邮！</p>
        <div class="nav-btn">
          <router-link to="/login" v-if="!isLogin">登录</router-link
          >
          <span v-if="isLogin">您好,</span>
          <router-link to="/my" v-if="isLogin">{{ username }}</router-link
          >
          <router-link to="/register/phone" v-if="!isLogin">注册</router-link
          > <a v-if="isLogin" @click="logoutFn">退出</a
          >
          <router-link to="/my" v-if="isLogin">我的订单</router-link
          > <a href="https://www.tp-link.com.cn/">TP-LINK官网</a
          >
          <!-- 购物车图标 -->
          <div class="shopCartIcon">
            <CartIcon></CartIcon>
          </div>
        </div>
      </div>
    </div>
    <!-- logo与搜索栏 -->
    <div class="logo-search">
      <!-- 网站logo -->
      <router-link to="/"
        ><img
          src="https://www.tp-linkshop.com.cn/Content/themes/base/images/Shared/logo.png"
      /></router-link>
      <!-- 搜索框 -->
      <div class="serach">
        <el-input
          placeholder="请输入您想搜的商品"
          prefix-icon="el-icon-search"
          v-model="input"
        >
        </el-input>
      </div>
    </div>
  </div>
</template>

<script>
import CartIcon from "../../components/shopping/CartIcon.vue";
export default {
  components: {
    CartIcon,
  },
  data() {
    return {
      input: "",
      username: "",
      isLogin: this.$store.state.token,
    };
  },
  methods: {
    // 注销
    logoutFn() {
      // 清空token与userInfo
      this.$store.commit("clearToken");
      alert("注销成功");
      // 注销后刷新本页
      this.$router.go(0);
    },
  },
  created() {
    // 将userInfo的name值传到username里
    this.username = JSON.parse(localStorage.getItem("userInfo")).name;
  },
};
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  min-width: 1200px;
  font-size: 12px;
  line-height: 40px;
  border-bottom: 1px solid #e6e6e6;
  box-shadow: 0 0 10px 0 rgb(0 0 0 / 4%);
  .nav {
    width: 100%;
    background-color: #333;
    .nav-info {
      width: 1200px;
      height: 40px;
      margin: 0 auto;
      display: flex;
      justify-content: space-between;
      align-items: center;
      p {
        display: block;
        color: #fff;
      }
      .nav-btn {
        color: rgba(255, 255, 255, 0.7);
        display: flex;
        justify-content: space-around;
        align-items: center;
        a {
          text-decoration: none;
          color: rgba(255, 255, 255, 0.7);
          padding: 0 8px;
          &:hover {
            color: #fff;
            cursor: pointer;
          }
        }
        .shopCartIcon {
          height: 40px;
          width: 80px;
          &:hover {
            background-color: #0096ff;
            color: #fff;
          }
        }
      }
    }
  }
  .logo-search {
    width: 1200px;
    height: 80px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      width: 237px;
    }
    .serach {
      width: 320px;
      height: 40px;
    }
  }
}
</style>
